<?php
/**
 * Created by PhpStorm.
 * User: gyccvvh
 * Date: 2018-06-13
 * Time: 17:31
 */
namespace app\merchant\model;
use think\Db;
use think\Model;

class Merchant extends Model
{
    public function getMerchantById($value) {
        return $this->getMerchantBy("id", $value);
    }

    public function getAuthMenu($user_id, $pid) {
        $records = Db::name("MerMenu")->where("pid", $pid)->alias("m")
            ->join("MerAuthRecord ar","ar.menu_id = m.id")
            ->join("MerAuthAso maa","maa.auth_id = ar.auth_id")
            ->where("maa.mer_id", $user_id)
            ->select();
        return $records;
    }

    public function hasAuth($mer_id, $rule) {
        $records = Db::name("MerAuthRecord")->where("rule_value", $rule)->alias("mar")
            ->join("MerAuthAso maa","maa.auth_id = mar.auth_id","LEFT")
            ->where("maa.mer_id",$mer_id)
            ->find();
        if($records){
            return true;
        }else {
            return false;
        }
    }

    public function checkForm($data, $scene = '') {
        $validate = validate($this->name);
        $res = $validate->scene($scene)->check($data);
        if (!$res) {
            return $validate->getError();
        } else {
            return false;
        }
    }

    public function getMerCert($mer_id) {
        $cert = Db::name("MerCert")->where("mer_id", $mer_id)->find();
        if($cert){
            return $cert;
        }else {
            return false;
        }
    }

    public function saveMerCert($_form, $id) {
        $_form['valid'] = 0;
        return Db::name("MerCert")->where("id",$id)->update($_form);
    }

    public function insertMerCert($_form) {
        $_form['valid'] = 0;
        return Db::name("MerCert")->insert($_form);
    }

     public function getMerDeposit($mer_id) {
        $deposit = Db::name("MerDeposit")->where("mer_id",$mer_id)->find();
        if($deposit) {
            return $deposit;
        }else {
            return false;
        }
     }

     public function getMerInfo($mer_id) {
        $info = Db::name("MerInfo")->where("mer_id",$mer_id)->find();
        if($info) {
            return $info;
        }else {
            return false;
        }
     }

     public function saveMerInfo($_form, $id) {
        $res =  Db::name("MerInfo")->where("id",$id)->update($_form);
        return $res;
     }

     public function insertMerInfo($_form)
     {
         return Db::name("MerInfo")->insert($_form);
     }

     public function saveMerStore($_form, $id) {
        $res = Db::name("MerStore")->where("id",$id)->update($_form);
        return $res;
    }
    public function insertMerStore($_form) {
        return Db::name("MerStore")->insert($_form);
    }

    public function getMerPay($mer_id) {
        $pay = Db::name("MerPay")->where("mer_id",$mer_id)->select();
        if($pay) {
            return $pay;
        }else {
            return false;
        }
     }

     public function getMerStore($mer_id) {
        $store = Db::name("MerStore")->where("mer_id",$mer_id)->find();
        if($store) {
            return $store;
        }else {
            return false;
        }
     }

    public function getMerchantBy($field, $value) {
        $map[$field] = $value;
        $merchant = $this->where($map)->find();
        if($merchant) {
            return $merchant->toArray();
        }else {
            return false;
        }
    }
}